import 'package:flutter/material.dart';
import 'package:qr_code_app/common/constant/color_config.dart';
import 'package:qr_code_app/generated/assets/qr_code_app_assets.dart';
import 'package:todo_flutter/todo_flutter.dart';

class CommonAppBar extends BaseStatelessWidget implements PreferredSizeWidget {
  final String title;
  final String? menu;
  final Color? menuColor;
  final bool safeArea;
  final Color? titleColor;
  final Color? backColor;
  final bool showBack;
  final VoidCallback? onBackTap;
  final VoidCallback? onMenuTap;
  final ScrollController? pageController;

  CommonAppBar(
    this.title, {
    this.pageController,
    super.key,
    this.menu,
    this.showBack = false,
    this.onBackTap,
    this.onMenuTap,
    this.safeArea = true,
    this.backColor = Colors.white,
    this.titleColor = ColorsConfig.ff393c42,
    this.menuColor = Colors.black,
  }) {
    pageController?.addListener(() {
      if (pageController!.position.pixels <= slideMaxY) {
        _pageSlideBloc.changeData(pageController!.position.pixels / slideMaxY);
      } else {
        _pageSlideBloc.changeData(1);
      }
    });
  }

  final slideMaxY = 120.0;

  final DataChangeBloc<double> _pageSlideBloc = DataChangeBloc(0);

  @override
  Widget build(BuildContext context) {
    if (ObjectUtil.isEmpty(pageController)) _pageSlideBloc.changeData(1);
    return DataChangeWidget(
      bloc: _pageSlideBloc,
      child: (BuildContext context, double? state) {
        final top = MediaQuery.of(context).padding.top;
        final double data = state! <= 0 ? 0 : state;
        return Container(
          height: preferredSize.height + (safeArea ? top : 0),
          decoration: BoxDecoration(color: Colors.white.withOpacity(data)),
          child: Column(
            children: [
              SizedBox(height: safeArea ? top : 0),
              SizedBox(
                height: preferredSize.height,
                child: Stack(
                  children: [
                    Center(
                      child: Container(
                        constraints: BoxConstraints(
                          maxWidth: setWidth(200),
                        ),
                        child: CommonText(
                          title,
                          fontSize: 16,
                          fontWeight: FontWeight.w500,
                          color: titleColor?.withOpacity(data),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                    Row(
                      children: [
                        Visibility(
                          visible: showBack,
                          child: Padding(
                            padding: only(left: 12),
                            child: IconButton(
                              iconSize: setWidth(20),
                              onPressed:
                                  onBackTap ?? () => RouterUtil.instance.pop(),
                              icon: Icon(Icons.arrow_back),
                            ),
                          ),
                        ),
                      ],
                    ),
                    Row(
                      children: [
                        const Spacer(),
                        CommonClickWidget(
                          onTap: () => onMenuTap?.call(),
                          child: Align(
                            child: Padding(
                              padding: only(right: 20),
                              child: CommonText(
                                menu ?? '',
                                fontSize: 14,
                                color: menuColor
                                    ?.withRed((255 * (1 - data)).toInt())
                                    .withBlue((255 * (1 - data)).toInt())
                                    .withGreen((255 * (1 - data)).toInt()),
                                fontWeight: FontWeight.w400,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  @override
  Size get preferredSize {
    return Size.fromHeight(setWidth(44));
  }
}
